﻿using System;
using System.Collections;
using Model;
using Resources;
using Service;

public partial class Frame_Equipment_EquipOtherInformationModify : System.Web.UI.Page
{
    readonly ReferenceDataService _ReferenceDataService = new ReferenceDataService();
    readonly  StockService _StockService = new StockService();
    Equipment _Equipment  = new Equipment();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadDataToComponents();
            int equipmentId = GetEquipmentId();

            pnlBody.Visible = equipmentId == 0 ? false : true;
            if (equipmentId == 0) return;

            if(equipmentId != 0)
            DisplayDetailInfo(equipmentId);
           // SetDisplayModeToComponents();

        }

    }
    private void IfPermissionDenie()
    {
        LoadDataToComponents();
        int equipmentId = GetEquipmentId();
        if (equipmentId != 0)
        DisplayDetailInfo(equipmentId);
        SetDisplayModeToComponents();
    }

    private int GetEquipmentId1()
    {
        return !String.IsNullOrEmpty(Request.QueryString["equipmentId"])? Int32.Parse(Request.QueryString["equipmentId"]) : 0;
    }
    public int GetEquipmentId()
    {
        if (Session["equipmentId"] != null) return Int32.Parse(Session["equipmentId"].ToString());
        return 0;
    }


    private int GetModelId()
    {
        return Request.QueryString["modelId"] != null ? Int32.Parse(Request.QueryString["modelId"]) : 0;
    }
    private void LoadDataToComponents()
    {
        drlBuilding.DataSource = _ReferenceDataService.GetBuildings();
        drlBuilding.DataValueField = Resource.BuildingValueField;
        drlBuilding.DataTextField = Resource.BuildingTextField;
        drlBuilding.DataBind();

        drlDepartment.DataSource = _ReferenceDataService.GetDepartments();
        drlDepartment.DataValueField = Resource.DepartmentValueField;
        drlDepartment.DataTextField = Resource.DepartmentTextField;
        drlDepartment.DataBind();

        drlLocation.DataSource = _ReferenceDataService.GetLocations();
        drlLocation.DataValueField = Resource.LocationValueField;
        drlLocation.DataTextField = Resource.LocationTextField;
        drlLocation.DataBind();

        drlEquipmentType.DataSource = _ReferenceDataService.GetEquipmentTypes();
        drlEquipmentType.DataTextField = Resource.EquipmentTypeTextField;
        drlEquipmentType.DataValueField = Resource.EquipmentTypeValueField;
        drlEquipmentType.DataBind();

        drlSystem.DataSource = _ReferenceDataService.GetSystems();
        drlSystem.DataValueField = Resource.SystemValueField;
        drlSystem.DataTextField = Resource.SystemTextField;
        drlSystem.DataBind();

        drlStockUser.DataSource = _StockService.GetStockEmployees();
        drlStockUser.DataTextField = Resource.EmployeeTextField;
        drlStockUser.DataValueField = Resource.EmployeeValueField;
        drlStockUser.DataBind();
    }
    private void DisplayDetailInfo(int equipmentId)
    {
        chkdepart.Checked = true;
        drlDepartment.Enabled = false;
        GetEmployees(0);

        _Equipment = _StockService.GetEquipment(equipmentId);

        chkIsDamaged.Checked = _Equipment.IsDamaged;

        // other information 
        txtSerialNumber.Text = !String.IsNullOrEmpty(_Equipment.SerialNumber) ? _Equipment.SerialNumber : string.Empty;
        txtEstimatedEOF.Text = _Equipment.EstimatedEOL.ToString(Resource.DateEnglishFormat);

        Equipment equipment = _StockService.GetEquipment(equipmentId);

        EquipmentOwner equipmentOwner = _StockService.GetEquipmentOwner(equipment.CodeNumber);
        // owner information 
        if (equipmentOwner != null)
        {
            drlBuilding.SelectedValue =
                equipmentOwner.BuildingId.ToString();

            drlLocation.SelectedValue =
                equipmentOwner.LocationId.ToString();

            drlEmployee.SelectedValue =
                equipmentOwner.EmployeeId.ToString();
            txtDateEnter.txtDate.Text = equipmentOwner.DateEnter.ToString(Resource.DateEnglishFormat);

            drlSystem.SelectedValue =
                equipmentOwner.SystemId.ToString();

           
              
            if(equipmentOwner.TypeId.ToString()=="0")
            {
                drlEquipmentType.SelectedIndex = -1; 
            }
            else
            {
                drlEquipmentType.SelectedValue = equipmentOwner.TypeId.ToString();
            }

        }

        else
        {
            drlBuilding.SelectedIndex = -1;
            drlLocation.SelectedIndex = -1;
            drlEmployee.SelectedIndex = -1;
        }
    }
    private void SetDisplayModeToComponents()
    {
        bool flagEnable = !String.IsNullOrEmpty(Request.QueryString["displayMode"])
                              ? Request.QueryString["displayMode"].Equals("true") ? false : true
                              :
                                  false;
       
        btnValidate.Visible = flagEnable;
       
    }

    private EquipmentOwner GetOwner(Equipment equip, EquipmentOwner _owner)
    {
        EquipmentOwner owner = _owner;
        
        owner.CodeNumber = equip.CodeNumber;
        owner.EmployeeId = chkstock.Checked == false
                               ? Int32.Parse(drlEmployee.SelectedValue)
                               : Int32.Parse(drlStockUser.SelectedValue);
            

        if (!chkstock.Checked)
        {
            owner.LocationId = Int32.Parse(drlLocation.SelectedValue);
            owner.BuildingId = Int32.Parse(drlBuilding.SelectedValue);
        }
        else
        {
            owner.LocationId = Int32.Parse(Resource.EquipLocationDefaultId);
            owner.BuildingId = Int32.Parse(Resource.EquipBuildingDefaultId);
        }

        owner.DateEnter = new Utility().ConvertToCustomizedFormat(txtDateEnter.txtDate.Text);
        owner.UnUse = chkstock.Checked;
        if (drlSystem.SelectedIndex>0)
        owner.SystemId = Int32.Parse(drlSystem.SelectedValue);
        if(drlEquipmentType.SelectedIndex >0)
        owner.TypeId = Int32.Parse(drlEquipmentType.SelectedValue);
        return owner;
    }
    protected void chkAllDepartment_CheckedChanged(object sender, EventArgs e)
    {
        drlDepartment.Enabled = !chkdepart.Checked;
        //display all employees of any departments
        if (chkdepart.Checked)
        {
            GetEmployees(0);

        }
        else if (drlDepartment.SelectedIndex != -1)
            GetEmployees(Int32.Parse(drlDepartment.SelectedValue));
    }
    protected void chkIsInStock_CheckedChanged(object sender, EventArgs e)
    {
        if (chkstock.Checked)
        {
            drlDepartment.Enabled = false;
            drlEmployee.Enabled = false;
            chkdepart.Checked = false;
            chkdepart.Enabled = false;
            // get stock users 
            

            drlLocation.Enabled = false;
            drlBuilding.Enabled = false;
            drlStockUser.Enabled = true;

        }
        else
        {
            chkdepart.Checked = true;
            chkdepart.Enabled = true;
            drlEmployee.Enabled = true;
            drlLocation.Enabled = true;
            drlBuilding.Enabled = true;
            drlStockUser.Enabled = false;

        }
    }
    protected void drlSystem_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drlSystem.SelectedIndex != -1)
        {
            IList equipTypeList = _ReferenceDataService.GetEquipmentTypes(Int32.Parse(drlSystem.SelectedValue));
            drlEquipmentType.DataSource = _ReferenceDataService.GetEquipmentTypeList(equipTypeList);
            drlEquipmentType.DataTextField = Resource.EquipmentTypeTextField;
            drlEquipmentType.DataValueField = Resource.EquipmentTypeValueField;
            drlEquipmentType.DataBind();

        }
    }
    private void GetEmployees(int departmentId)
    {
        if (chkdepart.Checked || departmentId == 0)
            drlEmployee.DataSource = _ReferenceDataService.GetEmployees();
        //display only employees of selected departments
        else drlEmployee.DataSource = _ReferenceDataService.GetEmployeesByDepartement(departmentId);

        drlEmployee.DataValueField = Resource.EmployeeValueField;
        drlEmployee.DataTextField = Resource.EmployeeTextField;
        drlEmployee.DataBind();

    }
    protected void drlDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drlDepartment.SelectedIndex != -1)
            GetEmployees(Int32.Parse(drlDepartment.SelectedValue));

    }
     private Equipment GetEquipment()
    {
        Equipment equipment = _StockService.GetEquipment(GetEquipmentId());
        equipment.SerialNumber =txtSerialNumber.Text;

        equipment.EstimatedEOL = new Utility().ConvertToCustomizedFormat(txtEstimatedEOF.Text);
           
        equipment.IsDamaged = chkIsDamaged.Checked;
        return equipment;
    }


   
    
protected void  btnValidate_Click(object sender, EventArgs e)
{
    //validate 
    if (!new Utility().TestPermissions(Resource.PermissionEditEquipmentOrtherInfo))
    {
        lblMessageValidate.Text = Resource.Permission;
        IfPermissionDenie();
        return;
    }

    if (chkstock.Checked)
    {
        if (drlStockUser.SelectedIndex == 0)
        {
            lblRequiredStockUser.Text = Resource.MessInformSelectAValue;
            lblRequiredStockUser.Visible = true;
            return;
        }
        lblRequiredStockUser.Visible = false;
    }
    //get equipment 
    Equipment equipment = GetEquipment();
    // get owner
    EquipmentOwner _owner = new StockService().GetEquipmentOwner(equipment.CodeNumber);
    EquipmentOwner owner = GetOwner(equipment, _owner);
  
    _StockService.UpdateEquipmentOwner(owner);
    _StockService.Update(equipment);

    lblMessageValidate.Text = Resource.MessageValidate; // Alert message validate 


    
}
protected void btnCancel_Click(object sender, EventArgs e)
{

}
}
